package com.ai.entity.made.camera;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
@Accessors(chain = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CameraSwitchAlgorithmBO implements Serializable {

    private int cameraId;
    private String username;
    private String password;
    private String ip;
    private String url;
    private int state;
    private String cameraCode;
    private String stripNo;
    private Integer status;
    private List<FunctionSwitchAlgorithmBO> functions;


    public static CameraSwitchAlgorithmBO convertPOToVO(CameraPO cameraPO) {
        CameraSwitchAlgorithmBO algorithmBO = new CameraSwitchAlgorithmBO();
        BeanUtils.copyProperties(cameraPO, algorithmBO);
        return algorithmBO;
    }

    public void setFunctions(List<FunctionSwitchAlgorithmBO> functions) {
        Map<String, FunctionSwitchAlgorithmBO> checkedMap = new HashMap();
        functions.forEach(function -> {

            JSONArray spot = JSON.parseArray(function.getSpot());
            String key = function.getType();
            if (checkedMap.containsKey(key)) {
                checkedMap.get(key).getSpots().add(spot);
            } else {
                function.setSpots(new JSONArray());
                function.getSpots().add(spot);
                checkedMap.put(key, function);
            }
            function.setSpot(null);
        });
        this.functions = checkedMap.values().stream().collect(Collectors.toList());
    }
}
